#include <iostream>
#include <iomanip> 
#include <cstdlib>
#include <ctime>    //用于使用time() 函数获取当前系统时间，以秒为单位
#include <conio.h>    //kbhit()    getch()	getche()
/*
    Name: class Calendar 
	Copyright: 
	Author: Quietism 
	Date: 08/05/16 20:28
	Description: 
*/
using namespace std;

class Calendar{
	private:
		int year,month,day,hour,minute,second;

		bool checkDate(int y, int m, int d){
			if(m > 12){
				cout<<"ERROR: Month > 12 ! "<<endl;
				return false;
			}
			if(y < 0){
				cout<<"ERROR: Year < 0 ! "<<endl;
				return false;
			}
			if(m < 0){
				cout<<"ERROR: Month < 0 ! "<<endl;
				return false;
			}
			if(d < 0){
				cout<<"ERROR: Day < 0 ! "<<endl;
				return false;
			}	
			if(isLeap(y)){
				if(m == 2 && d > 29){
					cout<<"ERROR: Leap Year February > 29  ! "<<endl;
					return false;
				}
			}else{
				if( m == 2 && d > 28 ){
					cout<<"ERROR: "<<y<<"is not a Leap Year, February > 28  ! "<<endl;
					return false;
				}
			}
			switch(y){
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					if(d > 31){	
						cout<<"ERROR: Month "<<y<<" Day > 31 !"<<endl;
						return false;			
					}break;
				case 4:
				case 6:
				case 9:
				case 11:
					if(d > 30){	
						cout<<"ERROR: Month "<<y<<" Day > 30 !"<<endl;
						return false;			
					}break;
			}
			return true;
		}
		
		bool checkTime(int h, int m, int s){
			if(h < 0){
				cout<<"ERROR: Hour < 0 ! "<<endl;
				return false;
			}
			if(m < 0){
				cout<<"ERROR: Minute < 0 ! "<<endl;
				return false;
			}
			if(s < 0){
				cout<<"ERROR: Second < 0 ! "<<endl;
				return false;
			}
			if(h > 24){
				cout<<"ERROR: Hour > 24 ! "<<endl;
				return false;
			}
			if(m > 60){
				cout<<"ERROR: Minute > 60 ! "<<endl;
				return false;
			}
			if(s > 60){
				cout<<"ERROR: Second > 60 ! "<<endl;
				return false;
			}
		}
		
		bool isLeap(int arg){
			return arg % 4 == 0 && arg % 100 !=0 || arg % 400 == 0;
		}
	public:
		Calendar(){
			time_t rawtime = time(0);
			struct tm * timeinfo = localtime( &rawtime ); 
			this->year = timeinfo -> tm_year+1900;
			this->month = timeinfo -> tm_mon+1;
			this->day = timeinfo -> tm_mday;
			this->hour = timeinfo -> tm_hour;
			this->minute = timeinfo -> tm_min;
			this->second = timeinfo -> tm_sec;
		}
		//yyyy, mm, dd, hh, mm, ss  
		Calendar(int y, int m, int d, int h, int mm, int ss){
			if(checkDate(y, m, d) && checkTime(y, m, d)){
				this->year = y;
				this->month = m;
				this->day = d;
				this->hour = h;
				this->minute = mm;
				this->second = ss;
			}else{
				cout<<"Using System Date and Time"<<endl;
				//exit(2);
			} 
			
			
		}
		//yyyy, mm, dd
		Date(int y, int m, int d){
			cout<<"Using System Time"<<endl;
			if(checkDate(y, m, d)){
				this->year = y;
				this->month = m;
				this->day = d;
			}else{
				cout<<"Using System Date"<<endl;
				//exit(2);
			} 
			
		}
		Time(int h, int mm, int ss){
			cout<<"Using System Date"<<endl;
			if(checkTime(h, mm, ss)){
				this->hour = h;
				this->minute = mm;
				this->second = ss;
			}else{
				cout<<"Using System Time"<<endl;
				//exit(2);
			} 
		} 
		void setYear(int arg){
			this->month = arg;
		}
		void setMonth(int arg){
			this->day = arg;
		}
		void setDay(int arg){
			this->hour = arg;
		}
		void setHour(int arg){
			this->minute = arg;
		}
		void setMinute(int arg){
			this->year = arg;
		}
		void setSecond(int arg){
			this->second = arg;
		}
		int getYear(){
			return this->year;
		}
		int getMonth(){
			return this->month;
		}
		int getDay(){
			return this->day;
		}
		int getHour(){
			return this->hour;
		}
		int getMinute(){
			return this->minute;
		}
		int getSecond(){
			return this->second;
		}
		void displayDate(){
			cout<<setfill('0');
			cout<<"DD/MM/YYYY"<<endl;
			cout<<setw(2)<<this->day<<"/"<<setw(2)<<this->month<<"/"<<setw(4)<<this->year<<endl;
		}
		void displayTime(){
			cout<<setfill('0');
			cout<<"HH:MM:SS"<<endl;
			cout<<setw(2)<<this->hour<<":"<<setw(2)<<this->minute<<":"<<setw(2)<<this->second<<endl;
		}
		void displayNow(){
			cout<<setfill('0');
			//cout<<"DD/MM/YYYY\tHH:MM:SS"<<endl;
			cout<<setw(2)<<this->day<<"/"<<setw(2)<<this->month<<"/"<<setw(4)<<this->year<<"\t"<<setw(2)<<this->hour<<":"<<setw(2)<<this->minute<<":"<<setw(2)<<this->second;//<<endl;
		}
		void Click(){  //设置秒为隔一秒进行更新时间
			setSecond((getSecond()+1)%60);
			if(getSecond()==0){
				setMinute((getMinute()+1)%60);
				if(getMinute()==0){		
			    	setHour(getHour()%12+1);
			    }
			}
		}
		
	};
	
int main(/*int argc, char*argv[]*/){
	Calendar calendar;
	time_t currentTime;
	
	int h,m,s;
    cout<<"Enter Current Hour:";
    cin>>h;
    cout<<"Enter Current Minute:";
    cin>>m;
    cout<<"Enter Current Second:";
    cin>>s;
    
	calendar.Time(h, m, s);
	cout<<"DD/MM/YYYY\tHH:MM:SS"<<endl;
	calendar.displayNow(); 
	while(true){  //以秒为单位进行更新时间
		currentTime=time(0);  //利用time(0)函数取得当前系统时间，以秒为单位
		while(time(0) < (currentTime+1));
		calendar.Click();
		if(kbhit() && getch()=='q'){
			return 0; // kbhit() 检测当前是否有键盘输入  getch()可用 getche() 替换，getche() 输入有回显 ,而getch() 输入没有回显
		} 
		cout<<"\r";
		calendar.displayNow(); 
    }
	return 0;
}
